//
//  Singleton.h
//  FlashAnimation
//
//  Created by chiawei.tseng on 12/12/24.
//
//

#ifndef Singleton_H
#define Singleton_H

#include <stddef.h>

template <typename T>
class Singleton
{
public :
    
    static inline T& Get();
    
    static inline bool Exists();
    
    static inline void Create();
    
    static inline void Release();

    virtual void Init() = 0 ;
    
protected :
    
    Singleton(){}
    
    Singleton(const Singleton&);                        // not allowed
    
    Singleton& operator=(const Singleton&);             // not allowed
    
private :
    
    static T* ms_instance;
};

template <typename T> T* Singleton<T>::ms_instance = NULL ;

template <typename T>
T& Singleton<T>::Get()
{
    return *ms_instance;
}

template <typename T>
bool Singleton<T>::Exists()
{
    return ms_instance != NULL;
}

template <typename T>
void Singleton<T>::Create()
{
    if (ms_instance == NULL)
    {
        ms_instance = new T;
        
        ms_instance->Init();
    }
}

template <typename T>
void Singleton<T>::Release()
{
    if (Exists())
    {
        T* instance = ms_instance;
        ms_instance = NULL;
        if(instance != NULL)
            delete instance;
    }
}



#endif
